from mod_ai import AIBrain
import mod_actor,mod_line
from items.guns import jb15Prototype,Shoot

class MarineBrain(AIBrain):
    def findRangedWeapon(self):
        for item in self.actor.items:
            for verb in item.verbs:
                if isinstance(verb,Shoot):
                    return item,verb
        return None
    
    def takeTurn(self):
        AIBrain.takeTurn(self)
        
        iteration = 1
        
        actor = self.actor
        finished = False
        while not finished:
            if iteration > 1000:
                raise Exception, "Infinite loop for Janitor"
            print 'In marine loop, iteration %s' % iteration
            iteration += 1
            
            level,origin = actor.location
            lastPlayerLoc = self.memory.lastPlayerLoc
            if lastPlayerLoc:
                playerLevelNum,playerCoords = lastPlayerLoc
                # Should change the inRange call to use the levelMemory.
                if playerLevelNum == level.number:
                    if mod_line.inRange(level,origin,playerCoords):
                        rangedWeaponInfo = self.findRangedWeapon()
                        if rangedWeaponInfo:
                            gun,shootVerb = rangedWeaponInfo
                            while actor.AP >= shootVerb.calculateAP(actor,gun,playerCoords):
                                shootVerb.doIt(actor,gun,playerCoords)
                        finished = True
                    else: # If the player is not in range...
                        finished = self.travel(playerCoords) # Finished if we didn't move
                else:
                    self.moveRandomly()
                    finished = True
            else:
                self.moveRandomly()
                finished = True

marinePrototype = mod_actor.ActorPrototype('MARINE', maxHP=5, maxAP=10, APperTurn=7, brainPrototype=MarineBrain)
marinePrototype.startingItemPrototypes = [jb15Prototype]
